#!/usr/bin/env python3
# -*- encoding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart, MIMEBase
from email.header import Header
from email.utils import parseaddr, formataddr
from email import encoders
import poplib
from email.parser import Parser


def sendEmail1():
    msg = MIMEText("hello, send by Dean", 'plain', 'utf-8')
    from_addr = input("From: ")
    password = input("Password: ")
    to_addr = input("To: ")
    smtp_server = input("SMTP server: ")
    server = smtplib.SMTP(smtp_server, 25)
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.close()


def sendEmail2():
    from_addr = input("From: ")
    password = input("Password: ")
    to_addr = input("To: ")
    smtp_server = input("SMTP server: ")
    msg = MIMEText("hello, send by Dean", 'plain', 'utf-8')
    msg['From'] = format_addr("Dean <%s>" % from_addr)
    msg['To'] = format_addr("pengxj <%s>" % to_addr)
    msg['Subject'] = Header("就是测试下主题而已啦", "utf-8").encode()
    server = smtplib.SMTP(smtp_server, 25)
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.close()


def sendHtmlEmail():
    from_addr = input("From: ")
    password = input("Password: ")
    to_addr = input("To: ")
    smtp_server = input("SMTP server: ")
    msg = MIMEMultipart()
    msg.attach(MIMEText("<div><span>以下是附件图片<span><img src='cid:0' " +
                        "style='width:500px'></div>", 'html', 'utf-8'))
    msg.attach(MIMEText("暂无法查看图片", 'plain', 'utf-8'))  # 防止无法查看html内容
    msg['From'] = format_addr("Dean <%s>" % from_addr)
    msg['To'] = format_addr("pengxj <%s>" % to_addr)
    msg['Subject'] = Header("就是测试下主题而已啦", "utf-8").encode()
    with open("E:\\pengxj\\工作\\其他\\学习\\python\\module\\1.bmp", "rb") as f:
        mine = MIMEBase("image", "bmp", filename="1.bmp")
        mine.add_header("Content-Disposition", "attachment", filename="1.bmp")
        mine.add_header("Content-ID", "<0>")
        mine.add_header("X-Attachment-Id", "0")
        mine.set_payload(f.read())
        encoders.encode_base64(mine)
        msg.attach(mine)
    server = smtplib.SMTP(smtp_server, 25)
    # server.starttls()  # 调用https
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.close()


def format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, "utf-8").encode(), addr))


# sendHtmlEmail()


def getEmail():
    email = input("Email:")
    password = input("Pwd:")
    pop_server = input("Server:")
    # server = poplib.POP3(pop_server)
    server = poplib.POP3_SSL(pop_server)
    server.set_debuglevel(1)
    print(server.getwelcome().decode("utf-8"))
    server.user(email)
    server.pass_(password)
    print("Message: %s , Size: %s" % server.stat())
    resp, mails, octets = server.list()
    print(mails)
    index = len(mails)
    resp, lines, octets = server.retr(index)
    msg_content = b"\r\n".join(lines).decode("utf-8")
    msg = Parser().parsestr(msg_content)
    print(msg)  # 必须进行适当的解码和展示
    # server.dele(index) # 删除
    server.quit()


getEmail()
